package com.bkgy.modular.tpdidata_tcm2280.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.tpdidata_tcm2280.dao.TPdiDatatcm2280Dao;
import com.bkgy.modular.tpdidata_tcm2280.entity.JTPdiData;
import com.bkgy.modular.tpdidata_tcm2280.entity.TPdiData_tcm2280;
import com.bkgy.modular.tpdidata_tcm2280.service.TPdiDataService;
import com.bkgy.sys.common.entity.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 酸轧2280PDI接口具体实现类
* @author  生成器生成
* @date 2025-03-29 17:07:41
*/
@Service
@Slf4j
public class TPdiDataServiceImpl implements TPdiDataService {
	
	@Autowired
	private TPdiDatatcm2280Dao tPdiDataDao;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTPdiData(String data,Long userId,String sname){
		try{
			JTPdiData jtPdiData = JSON.parseObject(data,JTPdiData.class);
			TPdiData_tcm2280 tPdiData = jtPdiData.getTPdiData();
    		//CodiUtil.newRecord(userId,sname,tPdiData);
            tPdiDataDao.insertDataTPdiData(tPdiData);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
            log.error(e.getMessage(),e);
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTPdiDataOne(Long indocno){
		try {
            tPdiDataDao.deleteDataTPdiDataOne(indocno);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTPdiDataMany(String str_id) {
        try {
        	String sql = "delete t_pdi_data where indocno in(" + str_id +")";
            tPdiDataDao.deleteDataTPdiDataMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTPdiData(String data,Long userId,String sname){
		try {
			JTPdiData jtPdiData = JSON.parseObject(data,JTPdiData.class);
			TPdiData_tcm2280 tPdiData = jtPdiData.getTPdiData();
        	//CodiUtil.editRecord(userId,sname,tPdiData);
            tPdiDataDao.updateDataTPdiData(tPdiData);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
           log.error(e.getMessage(),e);
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTPdiDataByPage(String data) {
        try {
        	JTPdiData jtPdiData = JSON.parseObject(data, JTPdiData.class); 
        	JSONObject jsonObject  = null;
        	Integer pageIndex = jtPdiData.getPageIndex();
        	Integer pageSize = jtPdiData.getPageSize();
        	
        	if(null == pageIndex || null == pageSize){
        		return ResultData.ResultDataFaultSelf("分页参数没有传", null); 
        	}
        	
        	if(null!=jtPdiData.getCondition()){
    			jsonObject  = JSON.parseObject(jtPdiData.getCondition().toString());
    		}
    
    		List<TPdiData_tcm2280> list = tPdiDataDao.findDataTPdiDataByPage((pageIndex-1)*pageSize, pageIndex*pageSize);
    		Integer count = tPdiDataDao.findDataTPdiDataByPageSize();
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTPdiDataByIndocno(String data) {
        try {
        	JTPdiData jtPdiData = JSON.parseObject(data, JTPdiData.class); 
        	Long indocno = jtPdiData.getIndocno();

			TPdiData_tcm2280 tPdiData = tPdiDataDao.findDataTPdiDataByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tPdiData);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }

	public ResultData findDataTPdiDataByEntID(String data){
		try {
			JTPdiData jtPdiData = JSON.parseObject(data, JTPdiData.class);
			JSONObject jsonObject  = null;
			if(null!=jtPdiData.getCondition()){
				jsonObject  = JSON.parseObject(jtPdiData.getCondition().toString());
			}
			TPdiData_tcm2280 tPdiData = tPdiDataDao.findDataTPdiDataByEntID(jsonObject);
			return ResultData.ResultDataSuccess(tPdiData);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
		}
	}
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TPdiData_tcm2280> findDataTPdiData(){
		List<TPdiData_tcm2280> list = tPdiDataDao.findDataTPdiData();
		return list;
	};
}
